package org.basic.sms.controller;

import java.util.List;

import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.basic.common.idempotent.annotation.RepeatSubmit;
import org.basic.common.web.core.BaseController;
import org.basic.sms.domain.bo.SysSmsChannelBo;
import org.basic.sms.domain.vo.SysSmsChannelVo;
import org.basic.sms.service.ISysSmsChannelService;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;

import org.basic.common.log.annotation.Log;

import org.basic.common.mybatis.core.page.PageQuery;
import org.basic.common.core.domain.R;
import org.basic.common.core.validate.AddGroup;
import org.basic.common.core.validate.EditGroup;
import org.basic.common.log.enums.BusinessType;
import org.basic.common.excel.utils.ExcelUtil;

import org.basic.common.mybatis.core.page.TableDataInfo;

/**
 * 邮件渠道
 *
 * @author Shao Hua
 * @date 2023-07-20
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/sms/channel")
public class SysSmsChannelController extends BaseController {

    private final ISysSmsChannelService sysSmsChannelService;


    @SaCheckPermission("sms:channel:list")
    @GetMapping("/listAll")
    public TableDataInfo<SysSmsChannelVo> list() {
        return sysSmsChannelService.queryAll();
    }

    /**
     * 查询邮件渠道列表
     */
    @SaCheckPermission("sms:channel:list")
    @GetMapping("/list")
    public TableDataInfo<SysSmsChannelVo> list(SysSmsChannelBo bo, PageQuery pageQuery) {
        return sysSmsChannelService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出邮件渠道列表
     */
    @SaCheckPermission("sms:channel:export")
    @Log(title = "邮件渠道", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(SysSmsChannelBo bo, HttpServletResponse response) {
        List<SysSmsChannelVo> list = sysSmsChannelService.queryList(bo);
        ExcelUtil.exportExcel(list, "邮件渠道", SysSmsChannelVo.class, response);
    }

    /**
     * 获取邮件渠道详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("sms:channel:query")
    @GetMapping("/{id}")
    public R<SysSmsChannelVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(sysSmsChannelService.queryById(id));
    }

    /**
     * 新增邮件渠道
     */
    @SaCheckPermission("sms:channel:add")
    @Log(title = "邮件渠道", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody SysSmsChannelBo bo) {
        return toAjax(sysSmsChannelService.insertByBo(bo));
    }

    /**
     * 修改邮件渠道
     */
    @SaCheckPermission("sms:channel:edit")
    @Log(title = "邮件渠道", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody SysSmsChannelBo bo) {
        return toAjax(sysSmsChannelService.updateByBo(bo));
    }

    /**
     * 删除邮件渠道
     *
     * @param ids 主键串
     */
    @SaCheckPermission("sms:channel:remove")
    @Log(title = "邮件渠道", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(sysSmsChannelService.deleteWithValidByIds(List.of(ids), true));
    }
}
